// Copyright (c) 2009 Mike Haight, Andy Kipp
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


#include "stdafx.h"
#include "Input.h"


using namespace HandBanana::Core;


Input::Input(boost::shared_ptr<Window> argWindow) : mWindow(argWindow), mAcquired(false), mCurrentKeyStates(mKeyStates1), mPreviousKeyStates(mKeyStates2), mDirectInputDevice(NULL), mDirectInputObject(NULL)
{
    // Initialize the staties
    {
        ::ZeroMemory(mKeyStates1, 256);
        ::ZeroMemory(mKeyStates2, 256);
    }

    HRESULT hResult;

    hResult = ::DirectInput8Create(
        ::GetModuleHandleW(NULL),
        DIRECTINPUT_VERSION,
        IID_IDirectInput8W,
        reinterpret_cast<void**>(&mDirectInputObject),
        NULL);

    THROW_IF_FAILED(hResult, "DirectInput8Create");

    hResult = mDirectInputObject->CreateDevice(
        GUID_SysKeyboard,
        &mDirectInputDevice,
        NULL);

    THROW_IF_FAILED(hResult, "IDirectInput8W::CreateDevice");

    hResult = mDirectInputDevice->SetDataFormat(&c_dfDIKeyboard);

    THROW_IF_FAILED(hResult, "IDirectInputDevice8W::SetDataFormat");
}


Input::~Input(void)
{
    Unacquire();

    SAFE_RELEASE(mDirectInputDevice);
    SAFE_RELEASE(mDirectInputObject);
}


void Input::Acquire(void)
{
    if (!mAcquired)
    {
        mDirectInputDevice->SetCooperativeLevel(
            mWindow->Handle(),
            DISCL_FOREGROUND | DISCL_NONEXCLUSIVE | DISCL_NOWINKEY);

        mDirectInputDevice->Acquire();
        mAcquired = true;
    }
}


void Input::Unacquire(void)
{
    if (mAcquired)
    {
        mDirectInputDevice->Unacquire();
        mAcquired = false;
    }
}


void Input::Update(void)
{
    mDirectInputDevice->GetDeviceState(256, (void**)mPreviousKeyStates);

    unsigned char* temp = mPreviousKeyStates;
    mPreviousKeyStates = mCurrentKeyStates;
    mCurrentKeyStates = temp;
}


bool Input::KeyDownState(int argKey) const
{
    return (mCurrentKeyStates[argKey] > mPreviousKeyStates[argKey]);
}


bool Input::KeyUpState(int argKey) const
{
    return (mCurrentKeyStates[argKey] < mPreviousKeyStates[argKey]);
}


bool Input::KeyPressedState(int argKey) const
{
    return (mCurrentKeyStates[argKey] != 0);
}